import datetime
import Findtime

class hasDate(object):

    """A simple object which represents an optional date parameter,
    including the special case then the date is None. Supports
    Findtime descriptors as constructors, or datetime objects. No date
    is specified with None, and is the default.

    >>> d = hasDate()
    >>> d.date

    >>> d.date = "5d"
    >>> d.date - datetime.date.today()
    datetime.timedelta(5)

    >>> d.date = datetime.date.today()
    >>> d.date - datetime.date.today()
    datetime.timedelta(0)

    >>> d.date = None
    >>> d.date

    >>> d = hasDate("01/01/2007")
    >>> d.date
    datetime.date(2007, 1, 1)

    >>> d = hasDate("Tomorrow")
    Traceback (most recent call last):
    ...
    ValueError: Unrecognized date description
    
    
    """

    def __init__(self, date_value = None):

        self._process_date(date_value)

    def _process_date(self, value):

        if not value:
            self._date = None
            return

        if isinstance(value, datetime.date):
            self._date = value
            return

        try_date = Findtime.make_date(value)
        if try_date:
            self._date = try_date
            return

        raise ValueError("Unrecognized date description")
            

    def _get_date(self):
        return self._date

    def _set_date(self, date_value):
        self._process_date(date_value)

    date = property(_get_date, _set_date)

if __name__ == "__main__":

    import doctest
    doctest.testmod()
